import time

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import unittest


class JiuXian(unittest.TestCase):

    def setUp(self) -> None:
        # 打开浏览器
        self.driver = webdriver.Firefox()
        # 双击打开百度页面
        self.driver.get('https://www.jiuxian.com/')
        time.sleep(1)
        # 窗口最大化
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)

    def tearDown(self) -> None:
        time.sleep(10)
        self.driver.quit()

    # @unittest.skip
    def test_jiuxian_01(self):
        # 点击请登录
        self.driver.find_element(By.CLASS_NAME, 'headerLogin').click()
        # 输入手机号
        self.driver.find_element(By.ID, 'userName').send_keys('15985645480')
        # 输入密码
        self.driver.find_element(By.ID, 'password').send_keys('gW09092434')
        # 强制等待 用户点击图片输入验证码
        time.sleep(20)
        # 点击登录按钮
        self.driver.find_element(By.NAME, 'bi_Usuallogin').click()
        # 点击确认
        self.driver.find_element(By.ID, 'bindSub').click()
        time.sleep(1)
        # 断言1：验证登录成功后断言：hi,jxw563943535
        dy1 = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div[1]/div/p/span').text
        self.assertEqual('jxw563943535', dy1)

    # 成功购买商品加入购物车，并生成待支付订单
    def test_jiuxian_02(self):
        # 调用登录成功的测试用例
        self.test_jiuxian_01()
        # 1 输入框输入剑南春
        self.driver.find_element(By.ID, 'wd').send_keys('剑南春')
        time.sleep(1)
        # 2 点击搜索按钮
        self.driver.find_element(By.CLASS_NAME, 'search-btn').click()
        # 向下滚动300像素
        self.driver.execute_script('window.scrollTo(0,300)')
        time.sleep(1)
        # 3 选择商品：52°剑南春500ml*6 整箱装
        self.driver.find_element(By.XPATH, '/html/body/div[6]/div/div[2]/div[5]/ul/li[2]/div[2]/div[1]/a/img').click()
        # 切换窗口
        self.driver.switch_to.window(self.driver.window_handles[-1])
        # 4 商品数量输入：2
        num = self.driver.find_element(By.XPATH, '//input[@id="_nub"]')
        # 清空输入框内容
        num.clear()
        time.sleep(2)
        num.send_keys('2')
        time.sleep(2)
        # 5 点击加入购物车
        self.driver.find_element(By.NAME, 'detail_jiarugouwuche').click()
        # 断言2：已成功加入购物车
        dy2 = self.driver.find_element(By.XPATH, '//*[@id="u-buy-layId"]/div/div[2]/span').text
        print(dy2)
        self.assertEqual('已成功加入购物车', dy2)
        # 6 点击查看购物车
        self.driver.find_element(By.XPATH, '//a[@href="https://cart.jiuxian.com"]').click()
        # 断言3：我的购物车
        dy3 = self.driver.find_element(By.CSS_SELECTOR, '.on').text
        print(dy3)
        self.assertEqual('我的购物车', dy3)
        # 断言4：52°剑南春500ml*6 整箱装
        dy4 = self.driver.find_element(By.XPATH,
                                       '//*[@id="cart_container"]/div[2]/div[3]/div[2]/div/div/div/div[2]/div/div[2]/div[1]/a').text
        print(dy4)
        self.assertEqual('52°剑南春500ml*6 整箱装', dy4)
        # 断言5：购物车 剑南春 商品数量 2
        dy5 = self.driver.find_element(By.XPATH,
                                       '//*[@id="cart_container"]/div[2]/div[3]/div[2]/div/div/div/div[5]/div/input').text
        print(dy5)
        self.assertEqual('2', dy5)
        # 7 点击去结算
        self.driver.find_element(By.LINK_TEXT, '去结算').click()
        # 断言……
        dy6 = self.driver.find_element(By.CLASS_NAME, 'on2').text
        print(dy6)
        # self.assertEqual('2', dy6)
        # 8 点击提交订单
        self.driver.find_element(By.CSS_SELECTOR, '#submitOrder').click()
        # 断言……
        dy7 = self.driver.find_element(By.CLASS_NAME, 'successCon').text
        print(dy7)
        # self.assertEqual('2', dy7)

    def test_jiuxian_03(self):
        # 点击请登录
        self.driver.find_element(By.CLASS_NAME, 'headerLogin').click()
        # 输入手机号
        self.driver.find_element(By.ID, 'userName').send_keys('15985645480')
        # 输入密码
        self.driver.find_element(By.ID, 'password').send_keys()
        # 强制等待 用户点击图片输入验证码
        time.sleep(20)
        # 点击登录按钮
        self.driver.find_element(By.NAME, 'bi_Usuallogin').click()
        # 断言3：
        time.sleep(2)
        kk = self.driver.find_element(By.CSS_SELECTOR,
                                      '#loginform > div > div > div > div > div.loginCon > div.loginType.loginUsual > div.Frame.enterFrame.wrong > div.mistakeTip').text
        self.assertEqual('请输入密码', kk)


if __name__ == '__main__':
    unittest.main()
